package com.alliky.core.log

import com.orhanobut.logger.AndroidLogAdapter
import com.orhanobut.logger.FormatStrategy
import com.orhanobut.logger.Logger
import com.orhanobut.logger.PrettyFormatStrategy

object KLog {
    private const val VERBOSE = 1
    private const val DEBUG = 2
    private const val INFO = 3
    private const val WARN = 4
    private const val ERROR = 5
    private const val NOTHING = 6

    //控制log等级
    private const val LEVEL = VERBOSE
    private var IS_SHOW = false
    private const val DEFAULT_TAG = "Logger"
    fun init(boo: Boolean) {
        IS_SHOW = boo
        init(boo, DEFAULT_TAG)
    }

    fun init(boo: Boolean, DEFAULT_TAG: String?) {
        IS_SHOW = boo
        val formatStrategy: FormatStrategy = PrettyFormatStrategy.newBuilder()
            .showThreadInfo(false) // (Optional) Whether to show thread info or not. Default true
            .methodCount(1) // (Optional) How many method line to show. Default 2
            .methodOffset(7) // (Optional) Hides internal method calls up to offset. Default 5
            .logStrategy(CustomLogCatStrategy()) // (Optional) Changes the log strategy to print out. Default LogCat
            .tag(DEFAULT_TAG) // (Optional) Global tag for every log. Default PRETTY_LOGGER
            .build()
        Logger.addLogAdapter(AndroidLogAdapter(formatStrategy))
    }

    fun v(tag: String?, message: String?) {
        if (IS_SHOW && LEVEL <= VERBOSE) {
            Logger.t(tag).v(message!!)
        }
    }

    fun d(tag: String?, message: Any?) {
        if (IS_SHOW && LEVEL <= DEBUG) {
            Logger.t(tag).d(message)
        }
    }

    fun d(message: Any?) {
        if (IS_SHOW && LEVEL <= DEBUG) {
            Logger.d(message)
        }
    }

    fun i(tag: String?, message: String?) {
        if (IS_SHOW && LEVEL <= INFO) {
            Logger.t(tag).i(message!!)
        }
    }

    fun w(tag: String?, message: String?) {
        if (IS_SHOW && LEVEL <= WARN) {
            Logger.t(tag).w(message!!)
        }
    }

    fun json(tag: String?, message: String?) {
        if (IS_SHOW && LEVEL <= WARN) {
            Logger.t(tag).json(message)
        }
    }

    fun e(tag: String?, message: String?) {
        if (IS_SHOW && LEVEL <= ERROR) {
            Logger.t(tag).e(message!!)
        }
    }
}